package com.dq.domain.goods;

import cn.hutool.extra.emoji.EmojiUtil;
import com.dq.context.Constant;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.domain.brand.Brand;
import com.dq.domain.category.Category;

import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.dq.vo.baseVo.VO.inCaseNull;

/**
 * @author liuhui
 * @date 2019/9/11 0011 13:46:25
 */
@Data
@Entity
@ApiModel("商品")
public class Goods extends BaseEntity implements VO.ToVO {
    @ApiModelProperty(value = "商品分类", hidden = true)
    @ManyToOne
    protected Category category;

    @ApiModelProperty(value = "商品编号", hidden = true)
    protected String goodsNo = generyStrAndDatetimeAndRandomNum("GD", Constant.ORDERS_SUFF_COUNT);
    @ApiModelProperty("商品标题")
    @NotBlank(message = "商品标题不能为空")
    protected String goodsName;

    @ApiModelProperty(value = "购买价格")
    @NotNull(message = "购买价格 不能为空")
    protected BigDecimal price;
    @ApiModelProperty(value = "市场价")
    @NotNull(message = "市场价 不能为空")

    protected BigDecimal originalPrice;
    @ApiModelProperty(value = "成本价")
    @NotNull(message = "成本价 不能为空")
    protected BigDecimal costPrice;

    @ApiModelProperty(value = "缩略图（主图）")
    protected String smallImgUrl;
    @ApiModelProperty(value = "展示图（多个）", hidden = true)
    protected String listImgUrl;

    @ApiModelProperty("商品简介")
    protected String introduce;
    @ApiModelProperty("商品详情")
    @Column(columnDefinition = "text")
//    @NotNull(message = "商品详情 不能为空")
    protected String goodsDescription;

    @ApiModelProperty("是否推荐")
    @NotNull(message = "是否推荐 不能为空")
    protected Boolean isRecommend;
    @ApiModelProperty("商品状态（上下架）")
    @Enumerated(EnumType.ORDINAL)
    @NotNull(message = "商品状态 （不能为空）")
    protected GoodsStatus goodsStatus;
    @ApiModelProperty("是否为积分商品")
    @NotNull(message = "是否为积分商品 不能为空")
    protected Boolean isIntegralGoods;

    @ApiModelProperty("兑换所需积分")
//    @NotNull(message = "兑换所需积分 不能为空")
    protected Long integralNum;

    @ApiModelProperty("是否小程序显示")
    @Enumerated(EnumType.ORDINAL)
    @NotNull(message = "是否小程序显示 不能为空")
    protected ShowStatus showStatus;

    @ApiModelProperty(value = "分享图", hidden = true)
    protected String shareImgUrl;
    @ManyToOne
    @ApiModelProperty(value = "商品品牌", hidden = true)
    protected Brand brand;

    @ApiModelProperty("排序")
    protected Long sort;


    @ApiModelProperty("仅用来展示门店商品库存")
    @Transient
    public Long goodsStock;
    @ApiModelProperty("仅用来展示商品库存中间表变更时间")
    @Transient
    public Long changeTime;
    @ApiModelProperty("仅用来展示")
    @Transient
    public Long orgId;
    @ApiModelProperty("仅用来展示")
    @Transient
    public String orgName;

    public enum GoodsStatus {
        lowerShelf("下架"),
        upperShelf("上架");
        public String name;

        GoodsStatus(String name) {
            this.name = name;
        }
    }

    public enum ShowStatus{
        smallShow("小程序上显示"),smallNotShow("小程序上不显示");
        public String name;
        ShowStatus(String name){
            this.name = name;
        }
    }


    @Data
    @ApiModel(value = "商品视图")
    public static class Vo implements VO {
        private Long id;
        @ApiModelProperty(value = "商品编号")
        private String goodsNo;
        @ApiModelProperty(value = "缩略图/主图")
        private String smallImgUrl;
        @ApiModelProperty(value = "列表图")
        private String listImgUrl;
        @ApiModelProperty(value = "分享图")
        private String shareImgUrl;
        @ApiModelProperty("商品标题")
        private String goodsName;
        @ApiModelProperty(value = "市场价")
        protected BigDecimal originalPrice;
        @ApiModelProperty(value = "成本价")
        private BigDecimal costPrice;
        @ApiModelProperty(value = "购买价格")
        private BigDecimal price;
        @ApiModelProperty("商品状态")
        protected String goodsStatusEn;
        protected String goodsStatusCn;
        @ApiModelProperty("排序")
        private Long sort;
        @ApiModelProperty("创建时间")
        private String createTime;
        @ApiModelProperty("商品详情描述")
        private String goodsDescription;
        @ApiModelProperty("介绍")
        protected String introduce;

        @ApiModelProperty("是否为积分商品")
        protected Boolean isIntegralGoods;
        @ApiModelProperty("兑换所需积分")
        protected Long integralNum;

        @ApiModelProperty("分类名称")
        public String categoryName;
        @ApiModelProperty("分类id")
        public Long categoryId;

        @ApiModelProperty(value = "商品品牌")
        private Brand.Vo brand;
    }
    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.goodsNo = this.goodsNo;
        vo.smallImgUrl = this.smallImgUrl;
        vo.listImgUrl = this.listImgUrl;
        vo.shareImgUrl = this.shareImgUrl;
        vo.goodsName = fmt(this.getGoodsName());
        vo.originalPrice = fmt(this.originalPrice);
        vo.costPrice = fmt(this.costPrice);
        vo.price = fmt(this.price);
        vo.goodsStatusEn = fmt(this.getGoodsStatus().toString());
        vo.goodsStatusCn = fmt(this.getGoodsStatus().name);
        vo.sort = this.sort;
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.goodsDescription = this.goodsDescription;
        vo.introduce = fmt(this.introduce);

        vo.isIntegralGoods = this.getIsIntegralGoods();
        vo.integralNum = fmt(integralNum);
        vo.categoryId = this.getCategory() == null ? null : this.getCategory().getId();
        vo.categoryName = this.getCategory() == null ? "" : this.getCategory().getTitle();

        vo.brand = isRecursion ? (Brand.Vo) inCaseNull(this.brand, false) : null;
        return vo;
    }

    /**
     * 关联库存/门店信息, 库存/门店set赋值
     */
    @Data
    @ApiModel("门店库存列表视图")
    public static class OrgStockListVo{
        protected Long id;
        @ApiModelProperty(value = "商品编号")
        protected String goodsNo;
        @ApiModelProperty("商品名称")
        protected String goodsName;
        @ApiModelProperty("购买价格")
        protected BigDecimal price;
        @ApiModelProperty("商品原价")
        protected BigDecimal originalPrice;
        @ApiModelProperty(value = "成本价")
        protected BigDecimal costPrice;

        @ApiModelProperty("是否推荐")
        protected Boolean isRecommend;
        @ApiModelProperty("商品状态")
        protected String goodsStatusEn;
        protected String goodsStatusCn;
        @ApiModelProperty("是否为积分商品")
        protected Boolean isIntegralGoods;
        @ApiModelProperty("兑换所需积分")
        protected Long integralNum;

        @ApiModelProperty("是否小程序显示")
        protected String showStatusEn;
        protected String showStatusCn;

        @ApiModelProperty("创建时间")
        protected String createTime;

        @ApiModelProperty("介绍")
        protected String introduce;
        @ApiModelProperty("商品详情")
        protected String goodsDescription;

        @ApiModelProperty(value = "缩略图（主图）")
        protected String smallImgUrl;
        @ApiModelProperty(value = "展示图（多个）")
        protected String listImgUrl;
        @ApiModelProperty(value = "展示图（多个）")
        protected List<String> listImgUrlList;

        @ApiModelProperty("门店商品库存, select值")
        protected Long goodsStock;
        @ApiModelProperty("SKU, set赋值")
        protected String sku;
        @ApiModelProperty("商品库存中间表变更时间, select值")
        protected String changeTime;
        @ApiModelProperty("select值")
        public Long orgId;
        @ApiModelProperty("select值")
        public String orgName;

        @ApiModelProperty("分类名称")
        public String categoryName;
        @ApiModelProperty("分类id")
        public Long categoryId;
        @ApiModelProperty("分类ids, set")
        public List<Long> categoryIdList;
        @ApiModelProperty("分类ids, set")
        public String categoryIdListStr;
    }

    public OrgStockListVo toOrgStockListVo(){
        OrgStockListVo orgStockListVo = new OrgStockListVo();
        orgStockListVo.id = this.getId();
        orgStockListVo.goodsNo = fmt(this.getGoodsNo());
        orgStockListVo.goodsName = fmt(this.getGoodsName());

        orgStockListVo.price = fmt(this.getPrice());
        orgStockListVo.originalPrice = fmt(this.getOriginalPrice());
        orgStockListVo.costPrice = fmt(this.getCostPrice());

        orgStockListVo.introduce = fmt(this.getIntroduce());
        orgStockListVo.goodsDescription = fmt(this.getGoodsDescription());
        orgStockListVo.smallImgUrl = fmt(this.getSmallImgUrl());
        orgStockListVo.listImgUrl = fmt(this.getListImgUrl());

        List<String> list = new ArrayList<>();
        if(StringUtils.isNotBlank(this.getListImgUrl())) {
            String[] split = this.getListImgUrl().split(",");
            list = Arrays.asList(split);
        }
        orgStockListVo.listImgUrlList = list;

        orgStockListVo.isRecommend = fmt(this.getIsRecommend());
        orgStockListVo.goodsStatusEn = fmt(this.getGoodsStatus().toString());
        orgStockListVo.goodsStatusCn = fmt(this.getGoodsStatus().name);
        orgStockListVo.isIntegralGoods = fmt(this.getIsIntegralGoods());
        orgStockListVo.integralNum = fmt(this.getIntegralNum());
        orgStockListVo.showStatusEn = this.getShowStatus() == null ? null : fmt(this.getShowStatus().toString());
        orgStockListVo.showStatusCn = this.getShowStatus() == null ? null : fmt(this.getShowStatus().name);

        orgStockListVo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        // 关联  库存
        orgStockListVo.goodsStock = fmt(this.getGoodsStock());
        orgStockListVo.changeTime = TimeUtil.longToString(this.changeTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        orgStockListVo.categoryId = this.getCategory() == null ? null : this.getCategory().getId();
        orgStockListVo.categoryName = this.getCategory() == null ? "" : this.getCategory().getTitle();
        orgStockListVo.orgId = fmt(this.getOrgId());
        orgStockListVo.orgName = fmt(this.orgName);

        return orgStockListVo;
    }

    public String getGoodsName() {
        if(StringUtils.isNotBlank(goodsName)) {
            return EmojiUtil.toUnicode(goodsName);
        }else {
            return goodsName;
        }
    }
    public void setGoodsName(String goodsName) {
        if(StringUtils.isNotBlank(goodsName)) {
            this.goodsName = EmojiUtil.toAlias(goodsName);
        }else {
            this.goodsName = goodsName;
        }
    }
}
